{layout name="layout/header" /}
<link href="root/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="root/js/plugins/fancybox/jquery.fancybox.css" rel="stylesheet">
<link href="root/css/plugins/summernote/summernote.css" rel="stylesheet">
<link href="root/css/plugins/summernote/summernote-bs3.css" rel="stylesheet">
<link href="root/css/plugins/switchery/switchery.css" rel="stylesheet">
<script src="root/js/plugins/switchery/switchery.js"></script>
<link href="root/css/plugins/datetimepicker/bootstrap-datetimepicker.css" rel="stylesheet">

<div class="wrapper wrapper-content animated fadeInRight" id="app">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>{$title}</h5>
                </div>
                <div class="ibox-content">
                    <form class="form-horizontal" id="form">
                        <div class="form-group" >
                            <label class="col-sm-2 control-label">奖品</label>
                            <div class="col-sm-10">
                                <select :disabled="id != 0" class="form-control m-b" name="default_id" v-model="default_id" @change="choose_prize()">
                                    <option value="0" >---请选择奖品---</option>
                                    <option :value="i.id"  v-for="i in config.prize">{{i.default_name}}</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">奖品名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" v-model="name" name="name">
                                <span class="help-block m-b-none">默认奖品名称和奖品图片都可以修改</span>
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-sm-2 control-label">类型</label>
                            <div class="col-sm-10">
                                <p class="form-control-static" v-if="type==1"><span class="text-danger">未中奖</span></p>
                                <p class="form-control-static" v-if="type==2"><span class="text-success">实物奖品</span></p>
                                <p class="form-control-static" v-if="type==3"><span class="text-info">虚拟奖品</span></p>
                            </div>
                        </div>
                        <div class="form-group" v-if="type>1">
                            <label class="col-sm-2 control-label">奖品数量</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" v-model="stock" name="stock" >
                                <span class="help-block m-b-none">剩余库存数量：<span class="text-danger">{{default_stock}}</span></span>
                            </div>
                        </div>
                        <div class="form-group" >
                            <label class="col-sm-2 control-label">中奖值</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" v-model="chance" name="chance" placeholder="取值范围为 1-2147483647">
                                <span class="help-block m-b-none">中奖概率计算公式：A/(A+B+C)，A,B,C为各个奖品所填入中奖值</span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">奖品图片</label>
                            <div class="col-sm-10">
                                <button class="btn btn-success product_upload_img" type="button">
                                    <i class="fa fa-upload"></i>&nbsp;&nbsp;<span class="bold">上传</span>
                                </button>
                                <input class="vue_img" type="file" style="display: none;" data-field="img"/>
                                <!--<span class="help-block m-b-none">比例612px*816px</span>-->
                            </div>
                            <div class="col-sm-10 col-sm-offset-2 ">
                                <div class="row" id="img">
                                    <div class="img_box col-xs-12 col-md-3" v-if="img!=''">
                                        <a class="fancybox" :href="img">
                                            <img alt="image" :src="img"/>
                                        </a>
                                        <a class="glyphicon glyphicon-trash" onclick="del_wx_img(this,'img');" :data-value="img"></a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <div class="col-sm-4 col-sm-offset-2">
                                <a class="btn btn-primary" @click="submit" type="submit">保存内容</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- iCheck -->
<script src="root/js/plugins/iCheck/icheck.min.js"></script>
<script src="root/js/plugins/validate/jquery.validate.min.js"></script>
<script src="root/js/plugins/validate/messages_zh.min.js"></script>
<script src="root/js/plugins/fancybox/jquery.fancybox.js"></script>
<script src="root/js/plugins/datetimepicker/bootstrap-datetimepicker.js"></script>
<script src="root/js/plugins/datetimepicker/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="root/js/plugins/summernote/summernote.min.js"></script>
<script src="root/js/plugins/summernote/summernote-zh-CN.js"></script>
<script>
    var data = {$data};
    var url = "{$url}";

    if (data == 1) {
        data = {
            id: 0,
            default_id: 0,
            lottery_id:"{$lottery.id}",
            // default_stock: 0,
            total_stock:0,
            name:"",
            img:"",
            type:0,
            chance:'',
            stock:"",
            enable:1,
        }
    }
    data.config= {$config};
    new Vue({
        el: "#app",
        data: data,
        computed:{
            default_stock:function () {
                if(this.stock !='' && !/^\d+$/.test(this.stock)){
                    temp = '必须是整数'
                }else if(this.stock > this.total_stock){
                    temp = '超出最大数量'
                }else {
                    temp = this.total_stock - this.stock;
                }
                return temp
            }
        },
        methods: {
            choose_prize:function(){
                var _this= this;
                $.each(_this.config.prize,function (k,v) {
                    if(_this.default_id == v.id){
                        _this.name = v.default_name;
                        _this.img = v.default_img;
                        _this.type = v.type;
                        _this.total_stock = v.stock;
                        return;
                    }
                })
            },

            submit:function () {
                var _this = this;
                $("#form").validate({
                    rules:{
                        default_id:{
                            min:1,
                        },
                        name:{
                            required:true,
                        },
                        stock:{
                            required:true,
                            digits:true,
                        },
                        chance:{
                            required:true,
                            digits:true,
                        },
                        type:{
                            min:1,
                        }

                    },
                    messages:{
                        default_id:{
                            min:"请选择奖品",
                        },
                    }
                })
                if($("#form").valid()) {
                    var _this = this;
                    if(_this.type != 1 && (_this.stock === '' || !/^\d+$/.test(this.default_stock))){
                        layer.msg('奖品数量不正确');
                        return false;
                    }
                    if(_this.img == ''){
                        layer.msg('请上传奖品图片');
                        return false;
                    }
                    $.post(url,data,function (res) {
                        layer.msg(res.msg);

                        if(res.code==1){
                            setTimeout(function () {
                                window.location.reload();
                            },1500)
                        }
                    },'json')
                }

            }
        },
        mounted: function () {
            checks();
            $('.fancybox').fancybox({
                openEffect: 'none',
                closeEffect: 'none'
            });
            $('.summernote').summernote({
                lang:'zh-CN',
                disableDragAndDrop: true,
                // 重写图片上传
                onImageUpload: function(files, editor, $editable) {
                    sendFile(files[0],editor,$editable);
                },
                onblur:function () {
                    var field = $(this).parent().prev().attr('data-field')
                    data[field] = $(this).code();
                }
            });
            $('.datetimepicker').datetimepicker({
                format:'yyyy-mm-dd',
                language:  'zh-CN',
                weekStart: 1,
                todayBtn:  1,
                autoclose: 1,
                todayHighlight: 1,
                startView: 2,
                maxView:3,
                minView:2,
                forceParse: 0,
            });
        }
    });
</script>

<script>
    $('input[name="type"]').on('ifChecked', function (event) {
        data.type = $(this).val()
    });
</script>
